package com.stanford.report;

import java.util.List;
import java.util.Map;

import com.stanford.data.DataUtil;
import com.stanford.data.User;
import com.stanford.ml.Statistics;

/**
 * Reports basic statistics like counts and frequencies of features
 * @author Fatih Sunor
 *
 */
public class Basic {

	private static final String STARS = "*************************************************************\n";
	private static final String BASIC_STATISTICS = "Basic Statistics for ";
	private static final int POS_RATING = 1;
	private static final int NEG_RATING = -1;
	private static final int NEU_RATING = 0;
	private static final String POS = "Positive Appreciation Ratio:\n";
	private static final String NEG = "Negative Appreciation Ratio:\n";
	private static final String NEU = "No Rating Ratio:\n";
	private static final String WITH = " with ";
	private static final String USERS = " users\n";
	private static final String MALE = "Male ratio:\n";
	private static final String FEMALE = "Female ratio:\n";
	private static final int MALE_INDEX = 1;
	private static final int CITY_TOP = 5;
	private static final int STATE_TOP = 5;
	private static final int COUNTRY_TOP = 5;
	private static final String CITY_TITLE = "\nMost Active Cities:";
	private static final String STATE_TITLE = "\nMost Active States:";
	private static final String COUNTRY_TITLE = "\nMost Active Countries:";
	private static final int TAG_TOP = 10;
	private static final String TAG_TITLE = "\nMost Active Tags:";
	private static final String CORRELATIONS = "Correlation with the rating for ";
	private static final double SIGNIFICANT = 0.1;
	
	public static String report(List<User> users, String webSite) {
		String result = STARS;
		result = result + BASIC_STATISTICS + webSite + WITH + users.size() + USERS;
		result = result + STARS + "\n\n";
		
		result = result + POS + Statistics.meanRating(users, POS_RATING)+"\n";
		result = result + NEG + Statistics.meanRating(users, NEG_RATING)+"\n";
		result = result + NEU + Statistics.meanRating(users, NEU_RATING)+"\n\n";
		
		double maleRatio = Statistics.genderRatio(users, MALE_INDEX);
		result = result + MALE + maleRatio + "\n";
		result = result + FEMALE + (1-maleRatio) + "\n";
		
		result = result + Plot.top(DataUtil.getCities(users, DataUtil.ALL), CITY_TOP, CITY_TITLE);
		result = result + Plot.top(DataUtil.getStates(users, DataUtil.ALL), STATE_TOP, STATE_TITLE);
		result = result + Plot.top(DataUtil.getCountries(users, DataUtil.ALL), COUNTRY_TOP, COUNTRY_TITLE);
		result = result + Plot.top(users, TAG_TOP, TAG_TITLE);
		return result;
	}

	public static String reportCorrelations(List<User> users, String webSite) {
		String result = "\n"+STARS;
		result = result + CORRELATIONS + webSite + WITH + users.size() + USERS;
		result = result + STARS + "\n";
		Map<String, Integer[]> factors = DataUtil.getAllFactors(users);
		for(String factor: factors.keySet()){
			double correlation = Statistics.correlation(factors.get(factor), DataUtil.getRatings(users, DataUtil.RATED_ONLY));
			if(Math.abs(correlation)>SIGNIFICANT){
				result = result + factor + ": " + correlation +"\n";
			}
		}
		return result;
	}
}
